﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.OfficeDocument;
using Enterprise.Data.OfficeDocument;
using Enterprise.Infrastructure;
namespace Enterprise.Service.OfficeDocument
{
    public class OfficeRecevieService
    {
        /// <summary>
        /// 用户缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-OfficeRecevie";

        /// <summary>
        ///  公文签收数据列表
        /// </summary>
        /// <returns></returns>
        public List<OfficeRecevieEntity> OfficeRecevieList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<OfficeRecevieEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                OfficeRecevieData data = new OfficeRecevieData();
                List<OfficeRecevieEntity> list = data.OfficeRecevieList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 判断人员是否签收会议记录
        /// </summary>
        /// <param name="mId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool OfficeRecevieCheck(string mId, int userId)
        {
            bool rbool = false;
            var q = OfficeRecevieList().Where(p => p.OID == mId && p.ORUSERID == userId).FirstOrDefault();
            if (q != null)
            {
                rbool = true;
            }
            return rbool;
        }

        /// <summary>
        /// 是否有签收权限
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool OfficeQianshouCheck(string qsUsers)
        {
            bool rbool = false;
            if (string.IsNullOrEmpty(qsUsers))
            {
                rbool = true;
            }
            else
            {
                string[] arr = qsUsers.Split(',');
                rbool = arr.Contains(Utility.Get_UserId.ToString());
            }
            return rbool;
        }

        /// <summary>
        /// 会议签收 添加、修改、删除
        /// </summary>
        /// <param name="entity">组织机构实体</param>
        public void OfficeRecevieExecute(OfficeRecevieEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into OFFICE_RECEVIE(ORID,OID,ORUSERID,ORTIME) values(SEQ_OFFICE_RECEVIE.nextval,'" + entity.OID + "'," + entity.ORUSERID + "," + Utility.FormatOracleDate(entity.ORTIME) + ")";
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                ExecuteBySql(cmd);
            }
        }

        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void ExecuteBySql(string cmd)
        {
            OfficeRecevieData data = new OfficeRecevieData();
            data.Execute(cmd);
            ClearCache();
        }
    }
}
